package xyz.weizhi.rjwm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import xyz.weizhi.rjwm.entity.Employee;
import xyz.weizhi.rjwm.entity.vo.R;
import xyz.weizhi.rjwm.service.EmployeeService;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author milian
 * @Date 2021/9/22 0022 15:34
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /**
     * @Author milian
     * @Description //账号登录
     * @Date 14:39 2021/9/23 0023
     * @Param [request, employee]
     * @return xyz.weizhi.rjwm.entity.vo.R<xyz.weizhi.rjwm.entity.Employee>
     **/
    @PostMapping("/login")
    public R<Employee> Login(HttpServletRequest request,@RequestBody Employee employee){
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(lqw);
        if (one==null){
            return R.error("无此用户");
        }
        if (!one.getPassword().equals(password)){
            return R.error("密码错误");
        }
        if (one.getStatus()==0){
            return R.error("此账号已被封禁");
        }
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }
    /**
     * @Author milian
     * @Description //账号登出
     * @Date 14:39 2021/9/23 0023
     * @Param [request]
     * @return xyz.weizhi.rjwm.entity.vo.R<java.lang.String>
     **/
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().invalidate();
        return R.success("退出成功");
    }
    /**
     * @Author milian
     * @Description //分页，查询获取员工列表
     * @Date 14:40 2021/9/23 0023
     * @Param [pageNum, pageSize, name]
     * @return xyz.weizhi.rjwm.entity.vo.R<com.baomidou.mybatisplus.core.metadata.IPage>
     **/
    @GetMapping("/page")
    public R<IPage>  getMemberList(@RequestParam("page") String pageNum,String pageSize,String name){
        IPage page = new Page(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(page,wrapper);
        log.info("pageasdaasd:{}",page);

        return R.success(page);
    }
    /**
     * @Author milian
     * @Description //根据id查询员工信息
     * @Date 14:40 2021/9/23 0023
     * @Param [id]
     * @return xyz.weizhi.rjwm.entity.vo.R<xyz.weizhi.rjwm.entity.Employee>
     **/
    @GetMapping("/{id}")
    public R<Employee> queryEmployeeById(@PathVariable("id") long id){

        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
    /**
     * @Author milian
     * @Description //新增员工
     * @Date 14:42 2021/9/23 0023
     * @Param [request, employee]
     * @return xyz.weizhi.rjwm.entity.vo.R<java.lang.String>
     **/
    @PostMapping
    public R<String> addEmployee(HttpServletRequest request, @RequestBody Employee employee){
        //获取创建人id
        //long id = (long) request.getSession().getAttribute("employee");
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //employee.setCreateUser(id);
        //employee.setUpdateUser(id);
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        boolean save = employeeService.save(employee);
        if (!save){
            return R.error("添加失败");
        }
        return R.success("添加成功");
    }
    /**
     * @Author milian
     * @Description //编辑员工
     * @Date 14:43 2021/9/23 0023
     * @Param [employee]
     * @return xyz.weizhi.rjwm.entity.vo.R<java.lang.String>
     **/
    @PutMapping
    public R<String> editEmployee(@RequestBody Employee employee,HttpServletRequest request){
        //long id = Thread.currentThread().getId();
        //long uid = (long) request.getSession().getAttribute("employee");
        log.info("emoloyee:{}",employee);
        //employee.setUpdateUser(uid);
        //employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
}
